package com.houcine.actions;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.houcine.entity.Author;
import com.houcine.entity.Book;
import com.houcine.services.GenericService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class AuthorAction extends FrontendAction {
	Logger logger = Logger.getLogger("logger");
	public String FORWORD_TO_UPDATE_PAGE = "update";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	//teste svn
	private GenericService<Author> service;
	private GenericService<Book> serviceBook;
	
	private String  firstName;
	private String  lastName;
	private String  blog;
	private Set<Book>  books = new HashSet<Book>();
	private String[]  listbooks ;
	private String  codeAuthors;
	private List<Author> authors;
    
    private Author author;
    
    private List<Book> availableBooks = new ArrayList<Book>();
    
    public AuthorAction(GenericService<Author> service,GenericService<Book> serviceBook) {
        this.service = service;
        this.serviceBook = serviceBook;
    }
    
    public String list() {
    	
        setAuthors(service.list(page, size));
        return SUCCESS;
    }
   
   
    public String create() {
    	 if (service == null) {
             return Action.ERROR;
         }
  
    	 logger.info("--------------teste--------------");
    	 logger.info(" books.size() : "+ books.size());
    	 
    	 Author author = new Author();
    	 author.setFirstName(firstName);
    	 author.setLastName(lastName);
    	 author.setBlog(blog);
    	 books = new HashSet<Book>();
    	 for (String id_book : listbooks) {
    		 logger.info("------------------------------------------id_book : "+id_book);
    		 Integer id = new Integer(id_book);
    		 logger.info("------------------------------------------id :"+id);
    		 Book book = serviceBook.get(id);
    		 books.add(book);
    	 }
    	 
    	 author.setBooks(books);
    	 logger.info("Add user: " + author.getFirstName());
         service.create(author);
        
       
        return SUCCESS;
    }
   

	public List<Author> getAuthors() {
		return authors;
	}

	public void setAuthors(List<Author> authors) {
		this.authors = authors;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getBlog() {
		return blog;
	}

	public void setBlog(String blog) {
		this.blog = blog;
	}

	public Set<Book> getBooks() {
		return books;
	}

	public void setBooks(Set<Book> books) {
		this.books = books;
	}

	public List<Book> getAvailableBooks() {
		setAvailableBooks(serviceBook.list(page, size));
		return availableBooks;
	}

	public void setAvailableBooks(List<Book> availableBooks) {
		this.availableBooks = availableBooks;
	}

	public String[] getListbooks() {
		return listbooks;
	}

	public void setListbooks(String[] listbooks) {
		this.listbooks = listbooks;
	}
	
	public String modifierAuthor(){
		logger.info("code auteur :"+codeAuthors);
		HttpServletRequest  request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		codeAuthors = request.getParameter("codeAuthors");
		if(null != codeAuthors && !"".equals(codeAuthors)){
			 Integer id = new Integer(codeAuthors);
			 author = service.get(id);
			 logger.info("--> chargement author :"+author);
			 firstName = author.getFirstName() ;
			 lastName  = author.getLastName();
			 blog      = author.getBlog();
			 books = author.getBooks();
			
		}
		
		
		return FORWORD_TO_UPDATE_PAGE;
	}
	
	 public String update(){
		 if (service == null) {
             return Action.ERROR;
         }
  
    	 logger.info("--------------teste--------------");
    	 logger.info(" author::: : "+ author);
    	 
    	 HttpServletRequest  request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
    	 codeAuthors = request.getParameter("codeAuthors");
    	 if(null != codeAuthors && !"".equals(codeAuthors)){
    		 Integer id = new Integer(codeAuthors);
 			 author = service.get(id);
 			 logger.info("--> chargement author :"+author);
 			 this.author.setFirstName(firstName);
 	    	 this.author.setLastName(lastName);
 	    	 this.author.setBlog(blog);
 	    	 books = new HashSet<Book>();
 	    	 for (String id_book : listbooks) {
 	    		 logger.info("------------------------------------------id_book : "+id_book);
 	    		 Integer idbook = new Integer(id_book);
 	    		 logger.info("------------------------------------------id :"+idbook);
 	    		 Book book = serviceBook.get(idbook);
 	    		 books.add(book);
 	    	 }
 	    	 
 	    	 this.author.setBooks(books);
 	    	 logger.info("update user: " + author.getFirstName());
 	         service.update(this.author);
 		}
    	
    	
		 return SUCCESS;
	 }
	 
	 
	 
	public String deleteAuthor(){
		logger.info("suppression auteur");
	
		HttpServletRequest  request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		codeAuthors = request.getParameter("codeAuthors");
		if(null != codeAuthors && !"".equals(codeAuthors)){
			 Integer id = new Integer(codeAuthors);
			 logger.info("--> id author to delete :"+id);
			 service.delete(id);
		}
		return SUCCESS;
	}

	public String getCodeAuthors() {
		return codeAuthors;
	}

	public void setCodeAuthors(String codeAuthors) {
		this.codeAuthors = codeAuthors;
	}
	
	
}
